import { BaseDeployPanel } from "./BaseDeployPanel";
import { Language } from "../../../../../../extend/language/Language";
import { GTextFormat } from "../../../../../common/GTextFormat";
import { FontUtil } from "../../../../../common/font/FontUtil";
import { EquipUpdateParams } from "../../data/EquipUpdateParams";
import { DataEvent } from "../../../../events/DataEvent";
import { EquipmentModuleConst } from "../../data/EquipmentModuleConst";
import { EBind } from "../../../../../../Message/Public/EBind";
import { ItemData } from "../../../../resource/info/ItemData";
import { EquipAdvanceConfig } from "../../../../resource/EquipAdvanceConfig";
import { Cache } from "../../../../cache/Cache";
import { ECategory } from "../../../../../../Message/Public/ECategory";
import { EItemUpdateResult } from "../../../../../../Message/Game/EItemUpdateResult";
import { MsgManager } from "../../../../manager/MsgManager";
import { ObjCreate } from "../../../common/ObjCreate";
import { GImageBitmap } from "../../../../../../com/mui/controls/GImageBitmap";
import { DeployPanelItem } from "../DeployPanelItem";
import { FilterConst } from "../../../../../component/gconst/FilterConst";
import { Alert } from "../../../../../../com/mui/controls/Alert";
import { EItemUpdateOper } from "../../../../../../Message/Game/EItemUpdateOper";
import { EColor } from "../../../../../../Message/Public/EColor";
import { GLabel } from "../../../../../../com/mui/controls/GLabel";
import { EquipRefreshInfo } from "../../../../resource/info/EquipRefreshInfo";
import { GButton } from "../../../../../../com/mui/controls/GButton";
type int = number;
//class EquipDecomposePanel
    
    export  class EquipDecomposePanel extends BaseDeployPanel
    {
        constructor()
        {
            this.currentEquipmentData = [];
            this.label_equip = Language.getString(60173);
            this.label_material = Language.getString(60174);
            this.posData = [{"x": 33, "y": 103, "label": this.label_equip, "canRemove": true, "noBoarder": false}, {"x": 93, "y": 103, "label": this.label_equip, "canRemove": true, "noBoarder": false}, {"x": 223, "y": 103, "label": this.label_equip, "canRemove": true, "noBoarder": false}, {"x": 283, "y": 103, "label": this.label_equip, "canRemove": true, "noBoarder": false}, {"x": 33, "y": 159, "label": this.label_equip, "canRemove": true, "noBoarder": false}, {"x": 93, "y": 159, "label": this.label_equip, "canRemove": true, "noBoarder": false}, {"x": 223, "y": 159, "label": this.label_equip, "canRemove": true, "noBoarder": false}, {"x": 283, "y": 159, "label": this.label_equip, "canRemove": true, "noBoarder": false}, {"x": 33, "y": 216, "label": this.label_equip, "canRemove": true, "noBoarder": false}, {"x": 93, "y": 216, "label": this.label_equip, "canRemove": true, "noBoarder": false}, {"x": 223, "y": 216, "label": this.label_equip, "canRemove": true, "noBoarder": false}, {"x": 283, "y": 216, "label": this.label_equip, "canRemove": true, "noBoarder": false}, {"x": 31, "y": 265, "label": this.label_material, "canRemove": false, "noBoarder": true}, {"x": 81, "y": 265, "label": this.label_material, "canRemove": false, "noBoarder": true}, {"x": 131, "y": 265, "label": this.label_material, "canRemove": false, "noBoarder": true}, {"x": 181, "y": 265, "label": this.label_material, "canRemove": false, "noBoarder": true}, {"x": 231, "y": 265, "label": this.label_material, "canRemove": false, "noBoarder": true}, {"x": 281, "y": 265, "label": this.label_material, "canRemove": false, "noBoarder": true}];
            this.buttonLabel = Language.getString(60175);
            this._tf = new GTextFormat(FontUtil.songtiName, 12, 16776960, true);
            this._alarmText1 = Language.getString(60176);
            this._alarmText2 = Language.getString(60177);
            super();
            return;
        }

        /* internal  */hasEquipsStrengthened(): Boolean
        {
            var loc1=0;
            while (loc1 < 12) 
            {
                if (allItem[loc1].itemData && allItem[loc1].itemData.itemExInfo.strengthen) 
                {
                    if (!(allItem[loc1].itemData.itemExInfo.strengthen == 1 && allItem[loc1].itemData.itemExInfo.perfectStrengthen < 100)) 
                    {
                        return true;
                    }
                }
                ++loc1;
            }
            return false;
        }

        /* internal  */hasEquipsEmbedJewel(): Boolean
        {
            var loc2=0;
            var loc3=0;
            var loc1=0;
            while (loc1 < 12) 
            {
                if (allItem[loc1].itemData) 
                {
                    loc2 = 1;
                    while (loc2 <= 4) 
                    {
                        loc3 = allItem[loc1].itemData.itemExInfo["hole" + loc2];
                        if (loc3 > 1) 
                        {
                            return true;
                        }
                        ++loc2;
                    }
                }
                ++loc1;
            }
            return false;
        }

        /* internal  */onConfirm(): void
        {
            var loc1=new Map<any, any> /* flash.utils.Dictionary */();
            var loc2=0;
            while (loc2 < 12) 
            {
                if (allItem[loc2].itemData) 
                {
                    loc1[allItem[loc2].itemData.uid] = 1;
                }
                ++loc2;
            }
            var loc3=new EquipUpdateParams();
            loc3.operationId = this.operationId;
            loc3.equipUidDic = loc1;
            this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_DoUpdateEvent1, loc3, false, true));
            return;
        }

        /* internal  */checkIsChangeToBind(): Boolean
        {
            if (!(allItem[0].itemData.itemExInfo.bind == EBind._EBindYes) && allItem[12].itemData.itemInfo.item.bind == EBind._EBindYes) 
            {
                return true;
            }
            return false;
        }

        protected /* override */ updateStatus(): void
        {
            var loc1=0;
            var loc3=null;
            var loc2=0;
            if (this.currentEquipmentData.length > 0) 
            {
                loc1 = 0;
                while (loc1 < this.currentEquipmentData.length) 
                {
                    loc3 = this.currentEquipmentData[loc1] as ItemData;
                    this.updateConfigInfo = EquipAdvanceConfig.instance.getDecomposeConfigInfo(loc3.itemInfo.item.color);
                    loc2 = loc2 + (this.updateConfigInfo ? this.updateConfigInfo.cost : 0);
                    ++loc1;
                }
            }
            else 
            {
                loc2 = 0;
            }
            if (_cost != loc2) 
            {
                _cost = loc2;
                dispatchEvent(new DataEvent(EquipmentModuleConst.Event_CostChange, _cost, false, true));
            }
            if (this.hasEquipsStrengthened()) 
            {
                this._remainCapacity.htmlText = Language.getString(60179) + Cache.instance.pack.backPackCache.getSpareCapacity();
                this._needCapacity.htmlText = Language.getString(60180) + this.countExProduce();
                this._remainCapacity.setStyle("textFormat", this._tf);
                this._needCapacity.setStyle("textFormat", this._tf);
                allItem[17].itemData = new ItemData(14031026);
            }
            else 
            {
                this._remainCapacity.htmlText = "";
                this._needCapacity.htmlText = "";
                allItem[17].itemData = null;
            }
            return;
        }

        /* internal  */countExProduce(): int
        {
            var loc1=0;
            var loc2=0;
            var loc3=0;
            var loc5=0;
            var loc4=new Map<any, any> /* flash.utils.Dictionary */();
            var loc6=0;
            while (loc6 < 12) 
            {
                if (allItem[loc6].itemData) 
                {
                    loc1 = allItem[loc6].itemData.itemExInfo.strengthen;
                    loc2 = allItem[loc6].itemData.itemExInfo.perfectStrengthen;
                    if (!(loc1 <= 0 || loc1 == 1 && loc2 < 100)) 
                    {
                        if (loc2 < 100) 
                        {
                            --loc3;
                        }
                        else 
                        {
                            loc3 = loc1;
                        }
                        if (!loc4[loc3]) 
                        {
                            loc4[loc3] = 1;
                            loc5 = loc5 + 1;
                        }
                    }
                }
                ++loc6;
            }
            return loc5;
        }

        public /* override */ putInItem(arg1: ItemData, arg2: Boolean=false, arg3=null): void
        {
            var loc1=0;
            if (arg1.itemInfo.item.category == ECategory._ECategoryEquip) 
            {
                loc1 = 0;
                while (loc1 < 12) 
                {
                    if (!this.checkIsHasPutin(arg1) && allItem[loc1].itemData == null) 
                    {
                        allItem[loc1].itemData = arg1;
                        this.currentEquipmentData.push(arg1);
                    }
                    ++loc1;
                }
                this.updateStatus();
                this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_ChoosedEquimentChange, this.currentEquipmentData, false, true));
            }
            return;
        }

        /* internal  */checkIsHasPutin(arg1: ItemData): Boolean
        {
            var loc1=0;
            while (loc1 < 12) 
            {
                if (allItem[loc1].itemData && allItem[loc1].itemData.uid == arg1.uid) 
                {
                    return true;
                }
                ++loc1;
            }
            return false;
        }

        public moveOutByIndex(arg1: int): void
        {
            if (arg1 >= 0 && arg1 < allItem.length && allItem[arg1].itemData) 
            {
                this.currentEquipmentData.splice(this.currentEquipmentData.indexOf(allItem[arg1].itemData), 1);
                allItem[arg1].itemData = null;
                this.updateStatus();
                this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_ChoosedEquimentChange, this.currentEquipmentData, false, true));
            }
            return;
        }

        public doEquipmentLost(arg1: Array<any>): void
        {
            var loc1=null;
            var loc2=0;
            var loc3=0;
            if (this.currentEquipmentData.length > 0) 
            {
                loc2 = 0;
                while (loc2 < arg1.length) 
                {
                    loc1 = arg1[loc2] as ItemData;
                    loc3 = 0;
                    while (loc3 < 12) 
                    {
                        if (allItem[loc3].itemData && allItem[loc3].itemData.uid == loc1.uid) 
                        {
                            this.moveOutByIndex(loc3);
                        }
                        ++loc3;
                    }
                    ++loc2;
                }
            }
            return;
        }

        public /* override */ clear(): void
        {
            clearAllItem();
            this.currentEquipmentData = [];
            this.updateStatus();
            this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_ChoosedEquimentChange, [], false, true));
            return;
        }

        public /* override */ setAutoChooseMaterial(arg1: Object): void
        {
            var loc1=arg1.material as Array<any>;
            var loc2=0;
            while (loc2 < 5) 
            {
                if (loc1.length > loc2) 
                {
                    allItem[12 + loc2].itemData = loc1[loc2] as ItemData;
                    allItem[12 + loc2].amount = (loc1[loc2] as ItemData).itemAmount;
                    allItem[12 + loc2].enabled = arg1.isEnough;
                }
                else 
                {
                    allItem[12 + loc2].itemData = null;
                }
                ++loc2;
            }
            this.updateStatus();
            return;
        }

        public /* override */ onUpdateResult(arg1: int, arg2: Boolean, arg3, arg4: Array<any>): void
        {
            if (arg1 != EItemUpdateResult._EItemUpdateResultSuccess) 
            {
                MsgManager.showRollTipsMsg(Language.getString(60183));
                this.clear();
            }
            else 
            {
                this.clear();
                MsgManager.showRollTipsMsg(Language.getString(60182));
            }
            return;
        }

        protected /* override */ createChildren(): void
        {
            var loc3=null;
            var loc4=null;
            super.createChildren();
            ObjCreate.createLabel(Language.getString(60178), 0, 66, 350, 25, flash.text.TextFormatAlign.CENTER, this, true, 16776960);
            this._remainCapacity = ObjCreate.createLabel(Language.getString(60179), 245, 60, 90, 20, flash.text.TextFormatAlign.LEFT, this, true, 16776960);
            this._needCapacity = ObjCreate.createLabel(Language.getString(60180), 245, 76, 90, 20, flash.text.TextFormatAlign.LEFT, this, true, 16776960);
            var loc1=new GImageBitmap("EquipDecomposeCircle", 141, 95);
            this.addChild(loc1);
            var loc2=0;
            while (loc2 < this.posData.length) 
            {
                loc3 = this.posData[loc2];
                (loc4 = new DeployPanelItem(loc2, loc3.noBoarder)).posIndex = loc2;
                loc4.x = loc3["x"];
                loc4.y = loc3["y"];
                loc4.label = loc3["label"];
                if (loc3.canRemove) 
                {
                    loc4.mouseEnabled = true;
                    loc4.addEventListener(flash.events.MouseEvent.CLICK, this.onItemClick);
                }
                addChild(loc4);
                allItem.push(loc4);
                ++loc2;
            }
            confirmButton.label = this.buttonLabel;
            confirmButton.x = 185;
            confirmButton.y = 307;
            confirmButton.width = 75;
            confirmButton.textField.filters = [FilterConst.glowFilter];
            confirmButton.styleName = "ButtonSpecial";
            addChild(confirmButton);
            confirmButton.addEventListener(flash.events.MouseEvent.CLICK, this.onButtonClick);
            this._autoSelectBtn = ObjCreate.createGButton(Language.getString(61053), 95, 307, 75, 26, this, "ButtonSpecial");
            this._autoSelectBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onAutoSelectButtonClick);
            return;
        }

        /* internal  */onItemClick(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=arg1.currentTarget as DeployPanelItem;
            this.moveOutByIndex(loc1.posIndex);
            return;
        }

        /* internal  */onButtonClick(arg1: Object /* flash.events.MouseEvent */): void
        {
            if (this.isEquipsNull()) 
            {
                MsgManager.showRollTipsMsg(Language.getString(60181));
                return;
            }
            if (!isEnoughMoney) 
            {
                this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_CoinNotEnough, null, false, true, true));
                MsgManager.showRollTipsMsg(Language.getString(60121));
                return;
            }
            if (this.hasEquipsEmbedJewel()) 
            {
                MsgManager.showRollTipsMsg(Language.getString(61054));
                return;
            }
            if (this.hasEquipsStrengthened()) 
            {
                Alert.show(this._alarmText1, null, Alert.OK | Alert.CANCEL, null, this.closeAlert);
            }
            else 
            {
                this.onConfirm();
            }
            return;
        }

        /* internal  */closeAlert(arg1: int): void
        {
            if (arg1 == Alert.OK) 
            {
                this.onConfirm();
            }
            return;
        }

        /* internal  */onAutoSelectButtonClick(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc2=0;
            var loc3=null;
            clearAllItem();
            this.currentEquipmentData = [];
            var loc1=Cache.instance.pack.backPackCache.getEquipItems();
            loc2 = 0;
            while (loc2 < loc1.length) 
            {
                if ((loc3 = loc1[loc2] as ItemData).category == ECategory._ECategoryEquip && this.isSuitableDecompose(loc3)) 
                {
                    this.currentEquipmentData.push(loc3);
                }
                if (this.currentEquipmentData.length >= 12) 
                {
                    break;
                }
                ++loc2;
            }
            loc2 = 0;
            while (loc2 < this.currentEquipmentData.length) 
            {
                allItem[loc2].itemData = this.currentEquipmentData[loc2] as ItemData;
                ++loc2;
            }
            this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_ChoosedEquimentChange, this.currentEquipmentData, false, true));
            return;
        }

        /* internal  */isSuitableDecompose(arg1: ItemData): Boolean
        {
            if (!Cache.instance.equipment.isSuitableEquipment(EItemUpdateOper._EItemUpdateDecompose, EquipmentModuleConst.EquipmentType_All, arg1)) 
            {
                return false;
            }
            if (!(arg1.category == ECategory._ECategoryEquip) || arg1.color < EColor._EColorGreen || arg1.color > EColor._EColorBlue) 
            {
                return false;
            }
            if (arg1.itemExInfo.strengthen > 3) 
            {
                return false;
            }
            if (arg1.itemExInfo.hole1 > 1 || arg1.itemExInfo.hole2 > 1 || arg1.itemExInfo.hole3 > 1 || arg1.itemExInfo.hole4 > 1 || arg1.itemExInfo.hole5 > 1 || arg1.itemExInfo.hole6 > 1) 
            {
                return false;
            }
            return true;
        }

        /* internal  */isEquipsNull(): Boolean
        {
            var loc1=0;
            while (loc1 < 12) 
            {
                if (allItem[loc1].itemData) 
                {
                    return false;
                }
                ++loc1;
            }
            return true;
        }

        /* internal */ /* var */currentEquipmentData: Array<any>;

        /* internal */ /* var */operationId: int=8;

        /* internal */ /* var */label_equip: string;

        /* internal */ /* var */label_material: string;

        /* internal */ /* var */posData: Array<any>;

        /* internal */ /* var */buttonLabel: string;

        /* internal */ /* var */_tf: Object /* flash.text.TextFormat */;

        /* internal */ /* var */_remainCapacity: GLabel;

        /* internal */ /* var */_needCapacity: GLabel;

        /* internal */ /* var */updateConfigInfo: EquipRefreshInfo;

        /* internal */ /* var */_alarmText1: string;

        /* internal */ /* var */_alarmText2: string;

        /* internal */ /* var */_autoSelectBtn: GButton;
    }
